from fastapi import APIRouter, Path, Depends
from sqlalchemy import text
from sqlalchemy.orm import Session

from commons.BaseResponse import BaseResponse
from datebase import get_db

data_alarm_set = APIRouter(prefix="/alarmSet", tags=["统一接口"])


@data_alarm_set.get("/airAlarm/listPointByFsiteNo2/{id}", summary="接口")
def getpointlist2(id: str = Path(), db: Session = Depends(get_db)):
    param = {"id": id}
    sql = "select fpoint_no,fpoint_name from a_ts_point"
    sql = sql + " where 1=1"
    if (id != "") and (id != "undefined"):
        sql = sql + " and fsite_no = :id "
    sql = sql + " and fpoint_flag=2"
    sql = sql + " order by fpoint_no"
    data = db.execute(text(sql), param).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fpointNo'] = data[key][0]
        d1['fpointName'] = data[key][1]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@data_alarm_set.get("/airAlarm/sTsAiritemOptions", summary="空气字典查询接口")
def list_air_item(db: Session = Depends(get_db)):
    sql = "select fairitem as value,fariname as label from s_ts_airitem where 1=1 order by fairitem"
    data = db.execute(text(sql)).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1["value"] = data[key][0]
        d1["label"] = data[key][1]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@data_alarm_set.get("/alarm/list", summary="报警记录--空气字典查询")
def list_alarm(db: Session = Depends(get_db)):
    sql = "select fairitem,fariname,ffield from s_ts_airitem where 1=1 order by fairitem"
    data = db.execute(text(sql)).fetchall()
    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fairitem'] = data[key][0]
        d1['fariname'] = data[key][1]
        d1['ffield'] = data[key][2]
        data_list.append(d1)
    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)
